<?php
/**
* base.php
*
* Create on 2015-8-26
* Create by TT
*/
namespace haibao\app\data;

class Base extends \haibao\app\data\BaseMysql{
	public function __construct($modelName){
		parent::__construct($modelName);
	}
	
	/**
	 * 得到实体对应的表名
	 * @param \haibao\app\model\data\TokenInfo $model
	 */
	public function setTableName($model){
		$tableName = $model->getTableName().date('Ym');
		$model->setNewTableName($tableName);
	}
	
	public function createApiRequestHistoryTable($tableName){
		$hasTableName = $this->checkHasTableName($tableName);
		if(!$hasTableName){
			$sql = "CREATE TABLE `".$tableName."` (
			  `Id` int(11) unsigned NOT NULL AUTO_INCREMENT,
			  `Token` varchar(50) DEFAULT '''''',
			  `Ticket` varchar(50) DEFAULT '''''' COMMENT '访问接口秘钥',
			  `Path` varchar(300) NOT NULL COMMENT '访问地址',
			  `Parameters` text COMMENT '参数',
			  `ReturnData` text NOT NULL COMMENT '返回内容',
			  `ReturnCode` int(11) unsigned NOT NULL COMMENT '返回状态码',
			  `UsageTime` varchar(20) NOT NULL COMMENT '耗时',
			  `IP` varchar(16) NOT NULL COMMENT 'IP地址',
			  `RequestTime` datetime DEFAULT NULL COMMENT '访问日期',
			  PRIMARY KEY (`Id`),
			  KEY `Token` (`Token`)
			) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;";
			$this->query($sql);
	
			$tableCache = new \haibao\app\cache\Table();
			$tableCacheName = $tableCache->setCache($tableName, $tableName);
		}
	}
	
	public function createUserLoginHistoryTable($tableName){
		$hasTableName = $this->checkHasTableName($tableName);
		if(!$hasTableName){
			$sql = "CREATE TABLE `".$tableName."` (
			  `Id` int(11) unsigned NOT NULL AUTO_INCREMENT,
			  `IMEI` varchar(50) NOT NULL,
			  `UserId` int(11) unsigned NOT NULL,
			  `CreateTime` datetime NOT NULL COMMENT '登录日期',
			  `Source` tinyint(4) NOT NULL COMMENT '1APP，2QQ，3WB，4WX',
			  `SysType` tinyint(4) DEFAULT '2' COMMENT '1：苹果，2：安卓',
			  `ActionType` tinyint(4) DEFAULT NULL COMMENT '1登录2注册',								
			  PRIMARY KEY (`Id`),
			  KEY `IMEi` (`IMEI`),
			  KEY `UserId` (`UserId`),
			  KEY `CreateTime` (`CreateTime`),
			  KEY `Source` (`Source`),
  			  KEY `SysType` (`SysType`),
  			  KEY `ActionType` (`ActionType`)
			) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;";
			$this->query($sql);
	
			$tableCache = new \haibao\app\cache\Table();
			$tableCacheName = $tableCache->setCache($tableName, $tableName);
		}
	}
	
	public function createTokenInfoTable($tableName){
		$hasTableName = $this->checkHasTableName($tableName);
		if(!$hasTableName){
			$sql="CREATE TABLE `".$tableName."` (
			  `Id` int(11) unsigned NOT NULL AUTO_INCREMENT,
			  `IMEI` varchar(50) NOT NULL,
			  `Token` varchar(50) NOT NULL,
			  `AppVersion` varchar(10) NOT NULL,
			  `CreateTime` datetime NOT NULL,
			  PRIMARY KEY (`Id`),
			  KEY `IMEI` (`IMEI`)
			) ENGINE=InnoDB AUTO_INCREMENT=619 DEFAULT CHARSET=utf8;";
			$this->query($sql);
	
			$tableCache = new \haibao\app\cache\Table();
			$tableCacheName = $tableCache->setCache($tableName, $tableName);
		}
	}
	
	public function createAppBootInfoTable($tableName){
		$hasTableName = $this->checkHasTableName($tableName);
		if(!$hasTableName){
			$sql="CREATE TABLE `".$tableName."` (
			  `Id` int(10) unsigned NOT NULL AUTO_INCREMENT,
			  `IMEI` varchar(50) NOT NULL,
			  `OSType` tinyint(1) unsigned NOT NULL COMMENT '操作系统（1：IOS、2：android、3：其它）',
			  `OSVersion` varchar(10) NOT NULL COMMENT '手机系统版本号',
			  `Network` varchar(50) DEFAULT NULL COMMENT '手机网络',
			  `AppVersion` varchar(10) NOT NULL COMMENT 'App应用版本号',
			  `CreateTime` datetime NOT NULL COMMENT '添加日期',
			  PRIMARY KEY (`Id`),
			  KEY `IMEI` (`IMEI`),
			  KEY `OSType` (`OSType`),
			  KEY `AppVersion` (`AppVersion`)
			) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;";
			$this->query($sql);
	
			$tableCache = new \haibao\app\cache\Table();
			$tableCacheName = $tableCache->setCache($tableName, $tableName);
		}
	}
	
	private function checkHasTableName($tableName){
		$tableCache = new \haibao\app\cache\Table();
		$tableCacheName = $tableCache->getCacheByKey($tableName);
		$hasTableName = true;
		if(empty($tableCacheName)){
			$sql = "SELECT table_name FROM information_schema.TABLES WHERE table_name ='".$tableName."'";
			$result = $this->query($sql);
			$hasTableName = false;
			while($result->fetch_assoc()){
				if($result->getData('table_name')){
					$hasTableName = true;
				}
			}
		}
		return $hasTableName;
	}
}